package geon.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import geon.model.Logradouro;

public class DaoLogradouro implements Dao<Logradouro> {

	private DaoGenerico dao;
	private String sql;
	
	public DaoLogradouro() {
		dao = new DaoGenerico();
	}
	
	@Override
	public Integer add(Logradouro o) throws SQLException {
		try {
			dao.begin();
			sql = "Insert Into TbLogradouro (LogCodigo, BaiCodigo, LogNome, LogCep) Values (?, ?, ?, ?)";
			o.setCodigo(dao.getNextId("TbLogradouro", "LogCodigo"));
			dao.executeCommand(sql, o.getCodigo(), o.getBairro().getCodigo(), o.getNome(), o.getCep());
			dao.commit();
		} catch (Exception e) {
			Logger.getLogger(getClass().getName()).log(Level.SEVERE, null, e);
			dao.rollback();
		}
		return o.getCodigo();
	}

	@Override
	public void upd(Logradouro o) throws SQLException {
		try {
			dao.begin();
			sql = "Update TbLogradouro Set BaiCodigo = ?, LogNome = ?, LogCep = ? Where LogCodigo = ?";
			dao.executeCommand(sql, o.getBairro().getCodigo(), o.getNome(), o.getCep(), o.getCodigo());
			dao.commit();
		} catch (Exception e) {
			Logger.getLogger(getClass().getName()).log(Level.SEVERE, null, e);
			dao.rollback();
		}
	}

	@Override
	public void del(int codigo) throws SQLException {
		try {
			dao.begin();
			sql = "Delete From TbLogradouro Where LogCodigo = ?";
			dao.executeCommand(sql, codigo);
			dao.commit();
		} catch (Exception e) {
			Logger.getLogger(getClass().getName()).log(Level.SEVERE, null, e);
			dao.rollback();
		}
	}

	@Override
	public Logradouro set(ResultSet rs) throws SQLException {
		DaoBairro bairro = new DaoBairro();
		Logradouro logradouro = new Logradouro();
		logradouro.setCodigo(rs.getInt("LogCodigo"));
		logradouro.setBairro(bairro.get(rs.getInt("BaiCodigo")));
		logradouro.setNome(rs.getString("LogNome"));
		logradouro.setCep(rs.getString("LogCep"));
		return logradouro;
	}

	@Override
	public Logradouro get(int codigo) throws SQLException {
		Logradouro logradouro = null;
		sql = "Select LogCodigo, BaiCodigo, LogNome, LogCep From TbLogradouro Where LogCodigo = ?";
		ResultSet rs = dao.executeQuery(sql, codigo);
		if (rs.next()) {
			logradouro = set(rs);
		}
		rs.close();
		return logradouro;
	}

	@Override
	public Logradouro get(Object param) throws SQLException {
		Logradouro logradouro = null;
		sql = "Select LogCodigo, BaiCodigo, LogNome, LogCep From TbLogradouro Where LogCep = ?";
		ResultSet rs = dao.executeQuery(sql, param);
		if (rs.next()) {
			logradouro = set(rs);
		}
		rs.close();
		return logradouro;
	}

	@Override
	public List<Logradouro> list() throws SQLException {
		List<Logradouro> logradouros = new LinkedList<Logradouro>();
		sql = "Select LogCodigo, BaiCodigo, LogNome, LogCep From TbLogradouro";
		ResultSet rs = dao.executeQuery(sql);
		while (rs.next()) {
			logradouros.add(set(rs));
		}
		rs.close();
		return logradouros;
	}

	@Override
	public List<Logradouro> list(Object... params) throws SQLException {
		List<Logradouro> logradouros = new LinkedList<Logradouro>();
		sql = "Select LogCodigo, BaiCodigo, LogNome, LogCep From TbLogradouro Where LogNome Like ?";
		ResultSet rs = dao.executeQuery(sql, "%" + params[0] + "%");
		while (rs.next()) {
			logradouros.add(set(rs));
		}
		rs.close();
		return logradouros;
	}

}
